package com.brainhealth.customer.service.impl;

import java.util.List;

import com.brainhealth.common.core.domain.entity.SysUser;
import com.brainhealth.common.utils.DateUtils;
import com.brainhealth.common.utils.SecurityUtils;
import com.brainhealth.common.utils.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.brainhealth.customer.mapper.CustomerLevelMapper;
import com.brainhealth.customer.domain.CustomerLevel;
import com.brainhealth.customer.service.ICustomerLevelService;

/**
 * 会员等级Service业务层处理
 * 
 * @author wolong
 * @date 2023-04-19
 */
@Service
public class CustomerLevelServiceImpl implements ICustomerLevelService 
{
    @Autowired
    private CustomerLevelMapper customerLevelMapper;

    /**
     * 查询会员等级
     * 
     * @param customerLevelId 会员等级主键
     * @return 会员等级
     */
    @Override
    public CustomerLevel selectCustomerLevelByCustomerLevelId(Long customerLevelId)
    {
        return customerLevelMapper.selectCustomerLevelByCustomerLevelId(customerLevelId);
    }

    /**
     * 用会员成长值获取会员级别
     * @param levelValue
     * @return
     */
    @Override
    public CustomerLevel selectCustomerLevelByValue(Long levelValue)
    {
        CustomerLevel customerLevel = new CustomerLevel();
        List<CustomerLevel> lstCustomerLevel = customerLevelMapper.selectCustomerLevelList(customerLevel);
        for (int i = lstCustomerLevel.size() - 1; i >= 0; i--)
        {
            CustomerLevel tmpLevel = lstCustomerLevel.get(i);
            if (levelValue >= tmpLevel.getLevelValue())
            {
                return tmpLevel;
            }
        }
        return null;
    }

    /**
     * 查询会员等级列表
     * 
     * @param customerLevel 会员等级
     * @return 会员等级
     */
    @Override
    public List<CustomerLevel> selectCustomerLevelList(CustomerLevel customerLevel)
    {
        return customerLevelMapper.selectCustomerLevelList(customerLevel);
    }

    /**
     * 新增会员等级
     * 
     * @param customerLevel 会员等级
     * @return 结果
     */
    @Override
    public int insertCustomerLevel(CustomerLevel customerLevel)
    {
        customerLevel.setCreateTime(DateUtils.getNowDate());
        return customerLevelMapper.insertCustomerLevel(customerLevel);
    }

    /**
     * 修改会员等级
     * 
     * @param customerLevel 会员等级
     * @return 结果
     */
    @Override
    public int updateCustomerLevel(CustomerLevel customerLevel)
    {
        customerLevel.setUpdateTime(DateUtils.getNowDate());
        return customerLevelMapper.updateCustomerLevel(customerLevel);
    }

    /**
     * 批量删除会员等级
     * 
     * @param customerLevelIds 需要删除的会员等级主键
     * @return 结果
     */
    @Override
    public int deleteCustomerLevelByCustomerLevelIds(Long[] customerLevelIds)
    {
        return customerLevelMapper.deleteCustomerLevelByCustomerLevelIds(customerLevelIds);
    }

    /**
     * 删除会员等级信息
     * 
     * @param customerLevelId 会员等级主键
     * @return 结果
     */
    @Override
    public int deleteCustomerLevelByCustomerLevelId(Long customerLevelId)
    {
        return customerLevelMapper.deleteCustomerLevelByCustomerLevelId(customerLevelId);
    }
}
